package com.example.demo.service.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Activity;
import com.example.demo.entity.ActivityWhoJoin;
import com.example.demo.entity.MyArray;
import com.example.demo.mapper.ActivityMapper;
import com.example.demo.mapper.ActivityWhoJoinMapper;
import com.example.demo.service.ActivityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    public ActivityMapper activityMapper;

    //添加商品
    @Override
    public int addActivity(Activity activity){
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.eq("activity_id", activity.getActivityId());
        List<Activity> res = activityMapper.selectList(wrapper);
        if (res != null && !res.isEmpty()) {
            return 0;
        } else {
            return activityMapper.insert(activity);
        }
    }

    @Override
    public int delActivity(MyArray activityId){
        List<Integer> list=new ArrayList<>();
        for(int id:activityId.getIds()){
            list.add(id);
        }
        return activityMapper.deleteBatchIds(list);
    }

    @Override
    public int updateActivity(Activity activity){
        QueryWrapper<Activity> wrapper=new QueryWrapper<>();
        wrapper.eq("activity_id",activity.getActivityId());
        List<Activity> res=activityMapper.selectList(wrapper);
        if(res!=null&&!res.isEmpty()){
            UpdateWrapper<Activity> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("activity_id",activity.getActivityId());
            return activityMapper.update(activity,updateWrapper);
        }else{
            return 0;
        }
    }

    @Override
    public List<Activity> selectAllActivities(){
        return activityMapper.selectList(null);
    }

    @Override
    public Activity selectActivityById(Integer activityId){
        QueryWrapper<Activity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("activity_id",activityId);
        List<Activity> res=activityMapper.selectList(queryWrapper);
        if(res!=null&&!res.isEmpty()){
            return res.get(0);
        }else{
            return null;
        }
    }

    @Resource
    private ActivityWhoJoinMapper activityWhoJoinMapper;


    public int joinActivity(Integer businessId, Integer activityId){
        List<ActivityWhoJoin> list = activityWhoJoinMapper.selectList(null);
        ActivityWhoJoin activityWhoJoin = new ActivityWhoJoin(businessId, activityId);
        if(list.contains(activityWhoJoin) == true){
            return 0;
        }
        else {
            return activityWhoJoinMapper.insert(activityWhoJoin);
        }
    }

    public int exitActivity(Integer businessId, Integer activityId){
        QueryWrapper<ActivityWhoJoin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",businessId);
        queryWrapper.eq("activity_id",activityId);
        return activityWhoJoinMapper.delete(queryWrapper);
    }
}
